package com.ysl.service;

import com.ysl.bean.Condition;
import com.ysl.bean.PageBean;
import com.ysl.bean.UserDemo;
import com.ysl.dao.UserDao;
import com.ysl.dao.UserDaoImpl;

import java.util.List;

public class UserServiceImpl implements UserService{
    UserDao userdao = new UserDaoImpl();
    @Override
    public boolean addUser(UserDemo user) throws Exception {
        int i = userdao.insertUser(user);
        return i>0 ? true:false ;
    }

    @Override
    public boolean findUser(String username, String userpwd) throws Exception {
        UserDemo user = userdao.selectUser(username, userpwd);
        return user != null ? true:false;
    }

    @Override
    public UserDemo findUserId(int id) throws Exception {
        UserDemo userDemo = userdao.selectUserId(id);
        return userDemo;
    }

    @Override
    public boolean modif(UserDemo user) throws Exception {
        int i = userdao.updateUser(user);
        return i >0 ? true:false;
    }

    @Override
    public boolean findUsername(String username) throws Exception {
        UserDemo userDemo = userdao.selectUserName(username);
        return userDemo != null? true:false;
    }

    @Override
    public boolean delUser(int id) throws Exception {
        int i = userdao.deleteUser(id);
        return i >0 ? true:false;
    }

    @Override
    public PageBean findUsers(Condition condition,Integer p) throws Exception {
        PageBean pageBean = new PageBean();
        Integer userDemoNum = userdao.selectUsersCount(condition);
        pageBean.setPageTotal(userDemoNum);
        pageBean.setP(p);
        List<UserDemo> userDemos = userdao.selectUsers(condition, pageBean.getP(), pageBean.getPageSize());
        pageBean.setUserDemos(userDemos);
        System.out.println(userDemos.isEmpty());
        return pageBean;
    }


}
